//
//  KYLoginModel.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/13.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYLoginModel.h"
#import "KYNetTool.h"
#import "KYLoginModel.h"
#import <MJExtension.h>

@implementation KYLoginModel
- (instancetype)init
{
    self = [super init];
    if (self) {
        [[self class]readData];
    }
    return self;
}
+(void)loginWithPhoneNo:(NSString *)phoneNo password:(NSString *)password success:(void(^)(BOOL))success{
    NSString *url = @"http://iosapi.itcast.cn/carelinkQuickLogin.json.php";
    NSDictionary *paramDic = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f",@"user_id":@(11),@"true_name":@"test",@"card_number":@"111",@"card_type":@(11)};
    [[KYNetTool shareManager]POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSDictionary *dataDict = dict[@"data"];
        KYLoginModel *loginModel = [KYLoginModel mj_objectWithKeyValues:dataDict];
        [loginModel saveData];

        success(YES);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error.description);
    }];
}
-(void)saveData{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"user.plist"];
    [NSKeyedArchiver archiveRootObject:self toFile:path];
    
}
+(KYLoginModel *)readData{
    //获取路径
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"user.plist"];
//    NSLog(@"%@",path);
    KYLoginModel *model = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (model){
        return model;
    }
    return nil;
}
/*
 @property (nonatomic, copy) NSString *true_name;
 @property (nonatomic, copy) NSString *head_photo;
 @property (nonatomic, copy) NSString *age;
 @property (nonatomic, copy) NSString *mobile_number;
 @property (nonatomic, copy) NSString *login_token;
 */
//归档
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:_true_name forKey:@"true_name"];
    [aCoder encodeObject:_head_photo forKey:@"head_photo"];
    [aCoder encodeObject:_age forKey:@"age"];
    [aCoder encodeObject:_mobile_number forKey:@"mobile_number"];
    [aCoder encodeObject:_login_token forKey:@"login_token"];
}
//解档
- (instancetype) initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init])
    {
        _true_name = [aDecoder decodeObjectForKey:@"true_name"];
        _head_photo = [aDecoder decodeObjectForKey:@"head_photo"];
        _age = [aDecoder decodeObjectForKey:@"age"];
        _mobile_number = [aDecoder decodeObjectForKey:@"mobile_number"];
        _login_token = [aDecoder decodeObjectForKey:@"login_token"];
    }
    return self;
}
//根据是否有login_token来判断是否已经登录
-(BOOL)isLogin{
    return self.login_token == nil ? NO : YES;
}
@end
